from flask import Blueprint, request, jsonify
from service import system_evolution_service
from utils import read_csv_file, read_text_file, encode_images

system_evolution_bp = Blueprint('system_evolution', __name__)

@system_evolution_bp.route('/core-process/train', methods=['POST'])
def train_model():
    scene = request.args.get("scene", "1")
    result = system_evolution_service.train_model(scene)
    return jsonify({"code": 200, "data": {"result": result}})

@system_evolution_bp.route('/core-process/predict', methods=['POST'])
def execute_prediction():
    scene = request.args.get("scene", "1")
    result = system_evolution_service.execute_prediction(scene)
    return jsonify({"code": 200, "data": {"result": result}})

@system_evolution_bp.route('/core-process/optimizread_text_file("system-evolution/e', methods=['POST'])
def optimize_model():
    scene = request.args.get("scene", "1")
    result = system_evolution_service.optimize_model(scene)
    return jsonify({"code": 200, "data": {"result": result}})

@system_evolution_bp.route('/result-review/prediction', methods=['GET'])
def get_prediction_results():
    content = read_text_file("system-evolution/predict_result.txt")
    return jsonify({"code": 200, "data": {"content": content}})

@system_evolution_bp.route('/result-review/preprocess', methods=['GET'])
def get_preprocess_results():
    content = read_text_file("system-evolution/preprocess_result.txt")
    return jsonify({"code": 200, "data": {"content": content}})

@system_evolution_bp.route('/result-review/model', methods=['GET'])
def get_prediction_models():
    content = read_text_file("system-evolution/model_info.txt")
    return jsonify({"code": 200, "data": {"content": content}})

@system_evolution_bp.route('/result-review/analysis', methods=['GET'])
def get_result_analysis():
    content = read_csv_file("system-evolution/analysis.csv")
    print(content)
    return jsonify({"code": 200, "data": {"content": content}})

@system_evolution_bp.route('/result-review/visualization', methods=['GET'])
def get_result_visualization():
    pre_image_path = [
        "system-evolution/output.png",
    ]
    images = encode_images(pre_image_path)
    return jsonify({"code": 200, "data": {"images": images}})

@system_evolution_bp.route('/result-review/comparison', methods=['GET'])
def get_dynamic_comparison():
    pre_image_path = [
        "system-evolution/output.png",
    ]
    images = encode_images(pre_image_path)
    return jsonify({"code": 200, "data": {"images": images}})

@system_evolution_bp.route('/system-analysis/rules', methods=['GET'])
def get_evolution_rules():
    content = read_text_file("system-evolution/evolution_rules.txt")
    return jsonify({"code": 200, "data": {"content": content}})

@system_evolution_bp.route('/system-analysis/history', methods=['GET'])
def get_historical_data():
    pre_image_path = [
        "system-evolution/output.png",
    ]
    images = encode_images(pre_image_path)
    return jsonify({"code": 200, "data": {"images": images}})